package com.etoc.test.flow.kd;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.etoc.test.util.DateUtil;
import com.etoc.test.util.EncryptUtil;
import com.etoc.test.util.HttpUtil;

/**
 * 
 * ��Դ�����ѯʵ����
 * <������ϸ����>
 * 
 * @author  �³�
 * @version  [�汾��, 2015-8-10]
 * @see  [�����/����]
 * @since  [��Ʒ/ģ��汾]
 */
public class QueryResourceOrder implements Serializable {
	/**
	 * ע������
	 */
	private static final long serialVersionUID = -570516968330765828L;
	private String action;
	private String appKey;
	private String appSecret;
	private String timeStamp;
	private String sign;
	private String phoneNo;
	private String model;
	private String startDate;
	private String endDate;
	Map<String, String> map = new TreeMap<String, String>();
	
	public QueryResourceOrder() {
		super();
	}

	public QueryResourceOrder(String action, String appKey, String appSecret, String phoneNo, String model,String startDate,String endDate,String sign) {
		super();
		this.action = action;
		this.appKey = appKey;
		this.appSecret = appSecret;
		this.sign = sign;
		this.phoneNo = phoneNo;
		this.model = model;
		this.startDate = startDate;
		this.endDate = endDate;
	}

	public String getAction() {
		return action;
	}
	
	public void setAction(String action) {
		this.action = action;
	}

	public String getAppKey() {
		return appKey;
	}

	public void setAppKey(String appKey) {
		this.appKey = appKey;
	}

	public String getAppSecret() {
		return appSecret;
	}

	public void setAppSecret(String appSecret) {
		this.appSecret = appSecret;
	}

	public String getTimeStamp() {
		return timeStamp;
	}

	public void setTimeStamp(String timeStamp) {
		this.timeStamp = timeStamp;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}
	
	public String getPhoneNo() {
		return phoneNo;
	}

	public void setPhoneNo(String phoneNo) {
		this.phoneNo = phoneNo;
	}
	
	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	
	public Map<String, String> getMap() {
		return map;
	}
	
	public String signString() {
		if (this.action != null && !this.action.equals(""))
		{
			map.put("action", this.action);
		}
		if (this.appKey != null && !this.appKey.equals(""))
		{
			map.put("appKey", this.appKey);
		}
		if (this.phoneNo != null && !this.phoneNo.equals(""))
		{
			map.put("phoneNo", this.phoneNo);
		}
		if (this.timeStamp != null && !this.timeStamp.equals(""))
		{
			map.put("timeStamp", this.timeStamp);
		}
		if (this.startDate != null && !this.startDate.equals(""))
		{
			map.put("startDate", this.startDate);
		}
		if (this.endDate != null && !this.endDate.equals(""))
		{
			map.put("endDate", this.endDate);
		}
		if (this.model != null && !this.model.equals(""))
		{
			map.put("model", this.model);
		}
		Iterator<String> keySet = map.keySet().iterator();
		StringBuffer signString = new StringBuffer(this.appSecret);
		while (keySet.hasNext())
		{
			String key = keySet.next();
			String val = map.get(key);
			signString.append(key).append(val);
		}
		signString.append(this.appSecret);
		return signString.toString(); 
	}
	
	@Override
	public String toString() {
		Iterator<String> keySet = map.keySet().iterator();
		String ret = "";
		while (keySet.hasNext())
		{
			String key = keySet.next();
			String val = map.get(key);
			ret += key + "=" + val + "&";
		}
		ret += "sign" + "=" + sign;
		return ret;
	}
	
	public static void main(String[] args) throws Exception
	{
		QueryResourceOrder order = new QueryResourceOrder();
		order.setAction("queryResourcePurchase");
		order.setAppKey("4aea47a6465e3de901465e68c8100000");
		order.setAppSecret("unicom");
		order.setPhoneNo("18671326122");
		order.setModel("0");
		order.setStartDate("20150901");
		order.setEndDate("20150930");
		order.setTimeStamp(DateUtil.dateToStr(new Date(), "yyyyMMddHHmmss"));
		String sign = order.signString();
		order.setSign(EncryptUtil.encryptSHA(sign));
		
		String url = "http://113.57.243.18:8070/flowAgent";
		System.out.println(url + "?" + order.toString());
		
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		Map<String, String> map = order.getMap();
		Iterator<String> keySet = map.keySet().iterator();
		while (keySet.hasNext())
		{
			String key = keySet.next();
			String val = map.get(key);
			
			nvps.add(new BasicNameValuePair(key, val));
		}
		nvps.add(new BasicNameValuePair("sign", order.getSign()));
		
		long startTime = System.currentTimeMillis();
		String respCode = HttpUtil.httpRequest(url, nvps);
		long endTime = System.currentTimeMillis();
		System.out.println("queryResourcePurchase=" + respCode);
		System.out.println("spend " + (endTime - startTime) + " milli second.");
		
		/*String data = "535FCF911F6D3865C39849305B776DD9023E30FB1DA820250486C484F87AFC0A6281883618831E2EAB8A3708A3CC3750164DFFE79060DD5C92A6632DAA487160C55A59FC6EABC82F164DFFE79060DD5C44DEB7F94BD24E794D0F989FE36AAC278A5EB39E508BF0849DA4CD95EB77594E5AE71FFA5B04B833857F5B88F84C86CE7B849B095C755C7DB86FDF7CB2A8DEFA949FF40989B32ACF1D50D45AC1BEA486164DFFE79060DD5C9FA894DB2228379F37FEB3E793705B5B8208A09A4C8F851FD400465D4A6EF8CB724EC4CE7E2FB54F23401576811B9807595EE03FA42EB8A576ACF84B582D94586A3168D4F47CEE9B0D4305FD2FD99CE1164DFFE79060DD5C92A6632DAA487160C55A59FC6EABC82F164DFFE79060DD5C44DEB7F94BD24E794D0F989FE36AAC278A5EB39E508BF0849DA4CD95EB77594E5AE71FFA5B04B833857F5B88F84C86CE7B849B095C755C7DB86FDF7CB2A8DEFA4EFD90E780D0C08321399AA7FC80AEE7BFDE294A3F24E5C32F1E96F3F7273538D2D0375F6D4D13942F405647A4DE15F58D6AAC2954469FE9A4ED70EF1E6122701D50D45AC1BEA486DBF5F7DA223435E238A3CB7D158853C164D6CC2349D9E442390F6363947047B1EDF967EA6F13B4BC";
		// cpPassword����С��8λ����Ĭ��ֵ12345678
		String result = EncryptUtil.decryptDES(data, "12345678", null);
		System.out.println(result);*/
	}
	
}
